# This program is for me to compute the X OR Y ranges for the 25 features.
# the input fileName for me to judge

# I do the following on 2013/04/06
# prof said that he think do some histogram is better.

# input option1
# for the X-Axis
# inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/queryTermPairProbabilityOfTheQueryTrace_sorted_by_query_term_pair_sorted_by_term_pair_freq"

# input option2
# for the Y-Axis
# inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/outputDirForTermScores/histogram_all_1000000TimesProbability_sorted_by_first_column_probability_increasing"

# input option3
# for testing purpose
# inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/outputDirForTermScores/histogram_for_testing_purpose.txt"

# input option4
# for constructing the query term probability table
inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/wholeLexiconTermsWithTermFreqInCollection_TermFreqInQLAdded_ProbabilityInQLAdded_DesidingFactorAdded_sortedByListLength.txt"

# output option1
# for the X-Axis
# outputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/queryTermPairProbabilityOfTheQueryTrace_sorted_by_query_term_pair_sorted_by_term_pair_freq" + "_with_designated_X_ranges_added"

# output option2
# This is optional output file
# for the Y-Axis
# outputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/outputDirForTermScores/histogram_all_1000000TimesProbability_sorted_by_first_column_probability_increasing" + "_with_designated_Y_ranges_added"

# output option3
# for testing purpose


# method value:
# value = 1: first compute the total number of postings, and divided by the number of ranges, and compute the specific ranges.
# value = 2: first compute the total number of lines(num of terms in this lexicon) in the file, and then divided by the number of ranges, and compute it
METHOD_VALUE = 2

NUM_OF_RANGES = 5

# for the X-Axis: the COLUMN_INDEX_TO_COMPUTE can be set to 2
# COLUMN_INDEX_TO_COMPUTE = 2
# for the Y-Axis: the COLUMN_INDEX_TO_COMPUTE can be set to 1
COLUMN_INDEX_TO_COMPUTE = 1

# First Pass: compute the total freq
totalFreqForMeToCompute = 0
rangeGap = 0
inputFileHandler = open(inputFileName,"r")

if METHOD_VALUE == 1:
    for line in inputFileHandler.readlines():
        lineElements = line.strip().split(" ")
        currentLineFreq = int( lineElements[COLUMN_INDEX_TO_COMPUTE] )
        totalFreqForMeToCompute += currentLineFreq
elif METHOD_VALUE == 2:
    totalFreqForMeToCompute = 37728619
 
inputFileHandler.close()
print "totalFreqForMeToCompute:",totalFreqForMeToCompute
rangeGap = int( totalFreqForMeToCompute/NUM_OF_RANGES )
print "rangeGap:",rangeGap

currentTotalFreqForMeToCompute = 0
currentRangeSet = 1
rangeSetDoneFlag = False
# Second Pass: design the ranges
inputFileHandlerAgain = open(inputFileName,"r")
# outputFileHandler = open(outputFileName,"w")
for index,line in enumerate( inputFileHandlerAgain.readlines() ):
    lineElements = line.strip().split(" ")
    
    # outputFileHandler.write( line.strip() + " " + str(currentRangeSet) + "\n")
    
    if not rangeSetDoneFlag:
        currentLineFreq = int( lineElements[COLUMN_INDEX_TO_COMPUTE] )
        currentTotalFreqForMeToCompute += currentLineFreq
        currentThreshold = currentRangeSet * rangeGap
        
        if currentTotalFreqForMeToCompute > currentThreshold:
            print "currentRangeSet:",currentRangeSet,"currentTotalFreqForMeToCompute:",currentTotalFreqForMeToCompute,"currentThreshold:",currentThreshold,"index:",index,"line:",line.strip()
            currentRangeSet += 1
            if currentRangeSet == 5:
                print "RangeSet DONE!"
                rangeSetDoneFlag = True
                # exit(1)
    else:
        # DO NOT NEED TO DO THIS LOGIC
        pass

inputFileHandlerAgain.close()
# outputFileHandler.close()




